package com.paradise.phone.utils;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by Sisyphus on 2017/5/11.
 */
public class MyItemDecoration extends RecyclerView.ItemDecoration {

    private static int DIVIDE_HIGHT = 2;

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);

        Paint pDevide = new Paint();
        pDevide.setColor(Color.parseColor("#f2f2f2"));
        int left = 0;
        int right = parent.getWidth();
        int size = parent.getChildCount();
        for (int i = 0; i < size - 1; i++){
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + DIVIDE_HIGHT;
            Rect rect = new Rect(left, top, right, bottom);
            c.drawRect(rect, pDevide);
        }

    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.top += DIVIDE_HIGHT;
        outRect.bottom += DIVIDE_HIGHT;
    }
}
